// pages/enterCommunity/enterCommunity.js
const { appInfo, urlInfo } = require("../../config/config.js");
const util = require('../../utils/util.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reg:0,//key值
    region:[],//原数据值
    regionArr:[],//循环数组
    sendData:{
    },
    area_key:0,//区域
    area:[],
    area_arr:[],

    build_key:0,//建筑
    build:[],
    build_arr:[],

    unit_key:0,//单元
    unit:[],
    unit_arr:[],

    room_key:0,//房间
    room:[],
    room_arr:[],
    memberid:'',

        ownerName:'',
        idCard:'',
        tel:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this;
    let userInfo=wx.getStorageSync('userInfo');
    if(userInfo){
      that.setData({memberid:userInfo.id});
    }else{
      app.showInfo('请登录','none',function(){
        wx.switchTab({
          url: '/pages/my/my'
        })
      });
    }
    //查询省级地区
    // let pros = this._loadArea('','');
  
    //获取小区
    app.post('region&op=list',{page:1,pageSize:200},function(rel){
      let regionArr=app.common.getArrObjFeild(rel.data.list,'title');//获取数组对象中的某个字段的值
      that.setData({region:rel.data.list,regionArr:regionArr});
    });
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let curLocation = wx.getStorageSync("currentLocation");
    this.setData({
      province: curLocation.province,
      city: curLocation.city,
     
        communityName: '',
        ownerName: '',
        idCard: '',
        tel: '',
        validateCode: ''
      
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

    bindRegionChange: function (e) {
      console.log('picker发送选择改变，携带值为', e.detail.value)
      // this.setData({
      //   region: e.detail.value
      // })
 
  },
  _loadArea:function(_level,_parentAreaCode){
    let that = this;
      wx.request({
        url: urlInfo.areaUrl,
        header: util.getHeaders(),
        data: {
          areaLevel: _level,                    // 临时登录凭证
          parentAreaCode: _parentAreaCode
        },
        success: function (res) {
          console.log('login success');
          res = res.data;
          var province = [], city = [], area = [];
          var _currentArea = [];
          province = res.areas.filter(item => {
            return item.parentAreaCode == 0;
          })
          city = res.areas.filter(item => {
            return item.parentAreaCode == province[0].areaCode;
          })
          area = res.areas.filter(item => {
            return item.parentAreaCode == city[0].areaCode;
          })
          var provinceList = province.map(item => {
            return item.areaName;
          })
          var cityList = city.map(item => {
            return item.areaName;
          })
          var quyuList = area.map(item => {
            return item.areaName;
          });
          let curLocation = wx.getStorageSync("currentLocation");
          _currentArea = res.areas.filter(item => {
            return item.areaCode == curLocation.adcode;
          })
          that.setData({
            multiArray: [provinceList, cityList, quyuList],
            areas: res.areas,
            currentArea: _currentArea[0],
            province
          });
          

        },

        fail: function (error) {
          // 调用服务端登录接口失败
          wx.showToast({
            title: '调用接口失败',
          });
          console.log(error);
        }
      })
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value,this.data);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    const provinceName = data.multiArray[0][data.multiIndex[0]];
    let provinceId = "";
    let province = this.data.province;
    let quyuList = [], cityList = [], provinceList = [], city = [], area = [];
    try {
      province.forEach(item => {
        if (item.areaName === provinceName) {
          provinceId = item.areaCode;
          throw (new Error('find item'))
        }
      })
    } catch (err) {
    }
    city = this.data.areas.filter(item => {
      return item.parentAreaCode == provinceId;
    })
    if (e.detail.column == 0) {
      data.multiIndex = [e.detail.value, 0, 0];
      try {
        area = this.data.areas.filter(item => {
          return item.parentAreaCode == city[data.multiIndex[1]].areaCode;
        })
      } catch (err) { }
    } else if (e.detail.column == 1) {
      data.multiIndex[2] = 0;
      area = this.data.areas.filter(item => {
        return item.parentAreaCode == city[e.detail.value].areaCode;
      })
    } else {
      const cityName = data.multiArray[1][data.multiIndex[1]];
      let cityId = '';
      try {
        this.data.areas.forEach(item => {
          if (item.areaName === cityName) {
            cityId = item.areaCode;
            throw (new Error('find item'));
          }
        })
      } catch (err) { }
      area = this.data.areas.filter(item => {
        return item.parentAreaCode == cityId;
        //return item.parentAreaCode == city[e.detail.value].areaCode;

      })
    }
    provinceList = province.map(item => {
      return item.areaName
    })
    cityList = city.map(item => {
      return item.areaName;
    })
    quyuList = area.map(item => {
      return item.areaName;
    });
    data.currentArea = area[data.multiIndex[2]];
    data.multiArray = [provinceList, cityList, quyuList],
      this.setData(data);
  },
  enterCommunity:function(e){
    let that=this;
    let data=that.data;
    let sendData={
      build:data.build[data.build_key].buildtitle,//建筑
      unit:data.unit[data.unit_key].unit,//单元
      room:data.room[data.room_key].room,//房间号
      roomid:data.room[data.room_key].id,//房间id
      regionid:data.region[data.reg].id,//小区ID
      regionname:data.region[data.reg].title,//小区名称
      memberid:data.memberid,//用户ID
      realname:data.ownerName,//正式姓名
      mobile:data.tel,//电话
      idCard:data.idCard//身份证号
    };
    app.post('index&op=bind',sendData,function(rel){
        console.log(rel);
    });
    console.log("提交数据",sendData);
  },
  bindChangeRegion:function(e){//选择小区
    let that    =this;
    let key     =e.detail.value;
    let regionid=that.data.region[key].id;
    that.setData({'changeRegion':that.data.region[key].title,reg:key,sendData:{regionid:regionid}});
    //区域
    app.post('region&op=area',{regionid:that.data.region[key].id},function(rel){ 
       let area_arr= app.common.getArrObjFeild(rel.data,'text');
        that.setData({'area':rel.data,'area_arr':area_arr,area_key:0});
    });
    //建筑
    app.post('region&op=build',{regionid:that.data.region[key].id},function(rel){
      let build_arr=app.common.getArrObjFeild(rel.data,'buildtitle');
      that.setData({'build':rel.data,'build_arr':build_arr,build_key:0});
    });
  },
  bindArea:function(e){//区域
    let that=this;
    let key=e.detail.value;
    let area=that.data.area[key].text;
    that.setData({area_key:key,sendData:{area:area}});
  },
  bindBuild:function(e){//建筑
    let that=this;
    let key=e.detail.value;
    let buildtitle=that.data.build[key].buildtitle;
    let buildid=that.data.build[key].id;
    that.setData({build_key:key,sendData:{build:buildtitle}});
    app.post('region&op=unit',{build:buildid},function(rel){
      let unit_arr=app.common.getArrObjFeild(rel.data,'unit');
      that.setData({unit:rel.data,unit_key:0,unit_arr:unit_arr});
    });
  },
  bindUnit:function(e){//选择单元
    let that=this;
    let key=e.detail.value;
    let unit=that.data.unit[key].unit;
    that.setData({unit_key:key,sendData:{unit:unit}});

    let data=that.data;
    let regionid=data.region[data.reg].id;
    let buildid =data.build[data.build_key].id;
    let buildtitle=data.build[data.build_key].buildtitle;
    let unitid  =data.unit[data.unit_key].id;

    app.post('region&op=room',{build:buildtitle,unit:unit,regionid:regionid},function(rel){
     let room_arr=app.common.getArrObjFeild(rel.data,'room');
         that.setData({room:rel.data,room_key:0,room_arr:room_arr});
    });
  },
  bindRoom:function(e){//选择房间
    let that=this;
    let key=e.detail.value;
    that.setData({room_key:key});
  },
  bindTel:function(e){
    let that=this;
    let val=e.detail.value;
    that.setData({tel:val});
  },
  bindIdCard:function(e){
    let that=this;
    let val=e.detail.value;
    that.setData({idCard:val});
  },
  bindOwnerName:function(e){
    let that=this;
    let val=e.detail.value;
    that.setData({ownerName:val});
  }

})